/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package test;

import java.io.*;
import java.util.*;
import java.sql.*;

public class TransactFormBean {

    public String amount;
    public String from;
    public String to;
    public String custid;
    public Hashtable errors;
    public DbConnect dbc;

    public boolean validate() {
        boolean bool = true;
        if (amount.equals("")){
            errors.put("amount", "Please Enter Amount Greater Than zero");
            amount ="";
            bool = false;
        }

        return bool;
    }

    public String getErrorMsg(String s) {
        String errorMsg = (String) errors.get(s.trim());
        return (errorMsg == null) ? "" : errorMsg;
    }

    public TransactFormBean() {
        amount ="";
        to = "";
        from = "";
        errors = new Hashtable();
    }

    public String getAmount() {
        return amount;
    }

    public void setFrom(String fname) {
        from = fname;
    }

    public void setTo(String lname) {
        to = lname;
    }

    public void setAmount(String l) {
        amount = l;
    }

    public boolean match() {

        boolean flag = false;
        Connection con = null;
        dbc = new DbConnect();
        String seq = null;
        int bal, count;


        try {

            con = dbc.getOracleConnection();
            Statement st = con.createStatement();

            PreparedStatement seq_ps = con.prepareStatement("select seq_trans.nextval from dual");
            ResultSet seq_rs = seq_ps.executeQuery();

            while (seq_rs.next()) {
                seq = seq_rs.getString(1);
            }

            ResultSet res = st.executeQuery("select balance from onbill_account where accno=" + "'" + from + "'" + "");
            res.next();
            if(Integer.parseInt(amount)<0){
                 errors.put("amount","Value Not Allowed");
                return flag;

            }
            bal = Integer.parseInt(res.getString(1)) - Integer.parseInt(amount);
            count = st.executeUpdate("insert into onbill_trans values(" + "'" + seq + "'" + "," + "'" + custid + "'" + "," + "'" + from + "'" + "," + "sysdate" + "," + "'" + bal + "'" + "," + "'" + to + "'" + ")");

            if (count > 0) {
                st.executeUpdate("update onbill_account set balance=" + "'" + bal + "'" + "where accno=" + "'" + from + "'" + "");
                flag = true;
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        return flag;
    }
}
